package com.qianyu.project.web.controller.lyz;

import com.qianyu.project.entity.MaterialInstorageRecord;
import com.qianyu.project.entity.MaterialInstorageRecordDetail;
import com.qianyu.project.service.IMaterialInstorageRecordService;
import com.qianyu.project.utils.Result;
import com.qianyu.project.utils.ResultTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author lyz
 * @date create in 2021/2/10 13:08
 */
@RestController
@RequestMapping("/MaterialInstorage")
public class MaterialInstorageController {
    @Autowired
    private IMaterialInstorageRecordService service;

    @GetMapping("")
    public Result getInstorageList(@RequestParam Map<String,Object> map){
        return ResultTool.success(service.list());
    }

    @PostMapping("")
    @Transactional
    public Result saveInstorage(@RequestBody Map<String,Object> map){
        System.out.println(map);
        Map<String,Object> storagemap= (Map<String, Object>) map.get("instorage");
        MaterialInstorageRecord instorage=new MaterialInstorageRecord();
        instorage.setProId(Integer.parseInt(storagemap.get("pId").toString()));
        instorage.setFlowId(1);
        instorage.setInstorageName(storagemap.get("instorageName").toString());
        instorage.setProviderId(Integer.parseInt(storagemap.get("providerId").toString()));
        instorage.setInstorageAmount(new BigDecimal(storagemap.get("instorageAmount").toString()));
        instorage.setApplyer(storagemap.get("applyer").toString());
        instorage.setDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        instorage.setBuyerId(Integer.parseInt(storagemap.get("buyerId").toString()));
        instorage.setDeliveryPerson(storagemap.get("deliveryPerson").toString());
        instorage.setAcceptorId(Integer.parseInt(storagemap.get("acceptorId").toString()));
        instorage.setInstorageId(Integer.parseInt(storagemap.get("instorageId").toString()));
        instorage.setComment(storagemap.get("comment").toString());
        instorage.setApproveStatus(1);

        List<Map<String,Object>> list= (List<Map<String, Object>>) map.get("details");
        List<MaterialInstorageRecordDetail> dlist=new ArrayList<MaterialInstorageRecordDetail>();
        list.forEach(e->{
            MaterialInstorageRecordDetail detail=new MaterialInstorageRecordDetail();
            detail.setInstorageId(Integer.parseInt(e.get("instorageId").toString()));
            detail.setMateId(Integer.parseInt(e.get("mateTypeId").toString()));
            detail.setWarehouseId(Integer.parseInt(e.get("warehouseId").toString()));
            detail.setInstorageCount(Integer.parseInt(e.get("instorageCount").toString()));
            detail.setNotaxPrice(new BigDecimal(e.get("notaxPrice").toString()));
            detail.setTaxRate(new BigDecimal(e.get("taxRate").toString()).divide(new BigDecimal(100)));
            detail.setTaxPrice(detail.getNotaxPrice().multiply(new BigDecimal(1).add(detail.getTaxRate())));
            detail.setTaxSumAmount(detail.getTaxPrice().multiply(new BigDecimal(detail.getInstorageCount())));
            detail.setNotaxSumAmount(detail.getNotaxPrice().multiply(new BigDecimal(detail.getInstorageCount())));
            dlist.add(detail);
        });
        System.out.println(instorage);
        System.out.println(dlist);
        service.saveInstorage(instorage,dlist);
        return ResultTool.success();
    }
}
